<template>
  <view class="comments-section">
    <view class="comments-header">
      <text class="comments-count">共有{{ commentsList.length }}条评论</text>
    </view>
    
         <!-- 评论列表 -->
     <view v-if="commentsList.length > 0" class="comments-list">
       <view 
         v-for="comment in commentsList" 
         :key="comment._id" 
         class="comment-item"
         :class="{ 'deleting': deletingCommentId === comment._id }"
         @longpress="handleLongPress(comment)"
       >
        <view class="comment-user-info">
          <image class="comment-avatar" :src="comment.author?.avatar || '/static/images/img/wxm.jpg'" mode="aspectFill"></image>
          <view class="comment-user-details">
            <text class="comment-username">{{ comment.author?.name || '匿名用户' }}</text>
          </view>
        </view>
        <view class="comment-content">
          <text class="comment-text">{{ comment.content }}</text>
        </view>
        <view class="comment-meta">
          <text class="comment-time">{{ formatCommentTime(comment.createdata) }}</text>
          <view class="comment-like" @click="handleLikeClick(comment)">
            <text class="like-icon" :class="{ 'liked': isUserLiked(comment) }">👍</text>
            <text class="like-count">{{ getLikeCount(comment) }}</text>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 无评论状态 -->
    <view v-else class="no-comments">
      <text class="no-comments-text">暂无留言哦~</text>
    </view>

    <!-- 长按弹出窗口 -->
    <view v-if="showActionSheet" class="action-sheet-overlay" @click="closeActionSheet">
      <view class="action-sheet" @click.stop>
        <view class="action-sheet-header">
          <text class="action-sheet-title">评论操作</text>
        </view>
        <view class="action-sheet-content">
          <view class="action-item" @click="handleReport">
            <text class="action-icon">🚨</text>
            <text class="action-text">举报</text>
          </view>
          <view class="action-item" @click="handleDelete">
            <text class="action-icon">🗑️</text>
            <text class="action-text">删除</text>
          </view>
          <view class="action-item" @click="handleShare">
            <text class="action-icon">↗️</text>
            <text class="action-text">分享</text>
          </view>
        </view>
        <view class="action-sheet-footer">
          <view class="cancel-btn" @click="closeActionSheet">
            <text class="cancel-text">取消</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { ref } from 'vue';

export default {
  name: 'DynamicDetailsComments',
  props: {
    // 评论列表
    commentsList: {
      type: Array,
      default: () => []
    }
  },
  emits: ['like-click', 'delete-comment'],
  setup(props, { emit }) {
    // 响应式数据
    const showActionSheet = ref(false);
    const selectedComment = ref(null);
    const deletingCommentId = ref(null); // 正在删除的评论ID
    // 格式化评论时间
    const formatCommentTime = (date) => {
      if (!date) return '刚刚';
      
      const now = new Date();
      const commentDate = new Date(date);
      const diffTime = Math.abs(now - commentDate);
      const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
      const diffHours = Math.ceil(diffTime / (1000 * 60 * 60));
      const diffMinutes = Math.ceil(diffTime / (1000 * 60));
      
      if (diffMinutes < 1) {
        return '刚刚';
      } else if (diffMinutes < 60) {
        return `${diffMinutes}分钟前`;
      } else if (diffHours < 24) {
        return `${diffHours}小时前`;
      } else if (diffDays < 7) {
        return `${diffDays}天前`;
      } else {
        return commentDate.toLocaleDateString();
      }
    };

    // 判断用户是否已点赞
    const isUserLiked = (comment) => {
      const userId = '689af77e90a9f6f3966d389d';
      // 确保 comment.like 是数组才调用 includes 方法
      return comment.like && Array.isArray(comment.like) && comment.like.includes(userId);
    };

    // 获取点赞数量
    const getLikeCount = (comment) => {
      // 确保 comment.like 是数组才获取长度
      return comment.like && Array.isArray(comment.like) ? comment.like.length : 0;
    };

    // 处理点赞点击
    const handleLikeClick = (comment) => {
      emit('like-click', comment);
    };

    // 处理长按
    const handleLongPress = (comment) => {
      selectedComment.value = comment;
      showActionSheet.value = true;
    };

    // 关闭操作面板
    const closeActionSheet = () => {
      showActionSheet.value = false;
      selectedComment.value = null;
    };

    // 处理举报
    const handleReport = () => {
      closeActionSheet();
      uni.showToast({
        title: '举报功能开发中',
        icon: 'none'
      });
    };

    // 处理删除
    const handleDelete = () => {
      if (!selectedComment.value) {
        uni.showToast({
          title: '评论数据异常',
          icon: 'none'
        });
        return;
      }
      
      // 检查是否正在删除
      if (deletingCommentId.value === selectedComment.value._id) {
        uni.showToast({
          title: '正在删除中，请稍候',
          icon: 'none'
        });
        return;
      }
      
      const commentToDelete = { ...selectedComment.value }; // 复制评论数据
      closeActionSheet();
      
      uni.showModal({
        title: '确认删除',
        content: '确定要删除这条评论吗？',
        success: (res) => {
          if (res.confirm) {
            deletingCommentId.value = commentToDelete._id;
            emit('delete-comment', commentToDelete);
          }
        }
      });
    };

    // 处理分享
    const handleShare = () => {
      closeActionSheet();
      uni.showToast({
        title: '分享功能开发中',
        icon: 'none'
      });
    };

    return {
      showActionSheet,
      selectedComment,
      formatCommentTime,
      isUserLiked,
      getLikeCount,
      handleLikeClick,
      handleLongPress,
      closeActionSheet,
      handleReport,
      handleDelete,
      handleShare
    };
  }
}
</script>

<style lang="scss" scoped>
/* 评论区域 */
.comments-section {
  background-color: #ffffff;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 15px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.comments-header {
  margin-bottom: 15px;
}

.comments-count {
  font-size: 16px;
  color: #333;
  font-weight: 600;
}

.comments-list {
  max-height: 450px;
  overflow-y: auto;
}

.comment-item {
  padding: 15px 0;
  border-bottom: 1px solid #f0f0f0;
}

.comment-item:last-child {
  border-bottom: none;
}

.comment-item.deleting {
  opacity: 0.6;
  pointer-events: none;
  position: relative;
}

.comment-item.deleting::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.8);
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.comment-item.deleting::after {
  content: '删除中...';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 12px;
  z-index: 2;
}

.comment-user-info {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.comment-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 10px;
  background-color: #f0f0f0;
}

.comment-user-details {
  flex: 1;
}

.comment-username {
  font-size: 14px;
  font-weight: 600;
  color: #333;
}

.comment-content {
  margin-bottom: 8px;
  padding-left: 42px;
}

.comment-text {
  font-size: 14px;
  color: #555;
  line-height: 1.5;
  word-wrap: break-word;
}

.comment-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 42px;
  font-size: 12px;
  color: #999;
}

.comment-time {
  margin-right: 15px;
}

.comment-like {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.like-icon {
  font-size: 14px;
  margin-right: 4px;
  transition: all 0.3s ease;
}

.like-icon.liked {
  color: #ff4757;
  transform: scale(1.2);
}

.like-count {
  font-size: 12px;
}

.no-comments {
  text-align: center;
  padding: 30px 0;
}

.no-comments-text {
  font-size: 14px;
  color: #999;
}

/* 操作面板样式 */
.action-sheet-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.action-sheet {
  width: 100%;
  background-color: #ffffff;
  border-radius: 12px 12px 0 0;
  overflow: hidden;
  animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: translateY(0);
  }
}

.action-sheet-header {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid #f0f0f0;
}

.action-sheet-title {
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.action-sheet-content {
  padding: 10px 0;
}

.action-item {
  display: flex;
  align-items: center;
  padding: 15px 20px;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
}

.action-item:active {
  background-color: #f0f0f0;
  transform: scale(0.98);
}

.action-item::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.05);
  opacity: 0;
  transition: opacity 0.2s ease;
}

.action-item:active::after {
  opacity: 1;
}

.action-icon {
  font-size: 18px;
  margin-right: 12px;
}

.action-text {
  font-size: 16px;
  color: #333;
}

.action-sheet-footer {
  padding: 10px 20px 30px;
}

.cancel-btn {
  width: 100%;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 8px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
  overflow: hidden;
}

.cancel-btn:active {
  background-color: #e9ecef;
  transform: scale(0.98);
}

.cancel-btn::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.05);
  opacity: 0;
  transition: opacity 0.2s ease;
}

.cancel-btn:active::after {
  opacity: 1;
}

.cancel-text {
  font-size: 16px;
  color: #666;
  position: relative;
  z-index: 1;
}
</style>